package io.renren.modules.money.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.R;
import io.renren.modules.money.mapper.MoneyMapper;
import io.renren.modules.money.model.Money;
import io.renren.modules.money.model.SearchRecord;
import io.renren.modules.money.service.MoneyService;
import io.renren.modules.shop.service.ShopManageService;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author A夜兔A
 * @data 2021年11月19日 9:35
 */
@Service
@Transactional
public class MoneyServiceImpl implements MoneyService {

    @Autowired
    private MoneyMapper moneyMapper;

    @Autowired
    private ShopManageService shopManageService;


    @Override
    public R changeMoney(Money money) {

        String tableNameChange = money.getShopNum() + "_customers";
        String tableNameRecord = money.getShopNum() + "_record";
        BigDecimal changeMoney = new BigDecimal("0");

        String remark = new String();
        if (money.getFlag() == 0) {
            changeMoney = money.getMoney();
            remark = "充值金额为" + changeMoney;
        } else {
            changeMoney = money.getMoney().negate();
            remark = "消费金额为" + money.getMoney();
        }

        moneyMapper.changeMoney(tableNameChange, money.getCustomerId(), changeMoney);

        moneyMapper.addRecord(tableNameRecord, money.getCustomerId(), changeMoney, new Date(), remark);
        return R.ok("操作成功");

    }

    @Override
    public R record(SearchRecord searchRecord) {
        String tableNameRecord = searchRecord.getShopNum() + "_record";
        String tableNameCustomer = searchRecord.getShopNum() + "_customers";
        SysUserEntity userEntity = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        Page<SearchRecord> page = new Page<>(searchRecord.getPageNum(), searchRecord.getPageSize());

        boolean admin = shopManageService.isAdmin();

        Page<SearchRecord> recordList = null;

        if (searchRecord.getCustomerId() == 0 && admin) {
            recordList = moneyMapper.getRecord(page,
                    tableNameRecord, tableNameCustomer,
                    searchRecord.getBeginDate(),
                    searchRecord.getEndDate(),
                    0L,
                    searchRecord.getCustomerId());
        } else if (searchRecord.getCustomerId() == 0 && !admin) {
            recordList = moneyMapper.getRecord(page,
                    tableNameRecord, tableNameCustomer,
                    searchRecord.getBeginDate(),
                    searchRecord.getEndDate(),
                    userEntity.getUserId(),
                    searchRecord.getCustomerId());

        } else if (searchRecord.getCustomerId() != 0 && admin) {
            recordList = moneyMapper.getRecord(page,
                    tableNameRecord, tableNameCustomer,
                    searchRecord.getBeginDate(),
                    searchRecord.getEndDate(),
                    0L,
                    searchRecord.getCustomerId());
        } else {
            return R.error("您无权访问其他人信息");
        }

        return R.ok("操作成功").put("recordList", recordList);
    }
}